package com.ymatou.userservice.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.userservice.parameter.UserRegisterTimeRequestDtoBean;
import com.ymatou.userservice.service.UserRegisterTimeRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.YmtUsers;
import com.ymttest.database.sqlwapper.LoginHistoryWapper;
import com.ymttest.database.sqlwapper.YmtUsersWapper;
import com.ymttest.utils.YMTDateUtil;
/**
*批量查询用户注册时间
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UserRegisterTimeRequestDto{
	private static UserRegisterTimeRequestDtoBean userregistertimerequestdtoBean;
	private static UserRegisterTimeRequestDtoCall userregistertimerequestdtoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量查询用户注册时间");
	}

	@Before
	public void caseUp() {
		userregistertimerequestdtoBean=new UserRegisterTimeRequestDtoBean();
		userregistertimerequestdtoCall=new UserRegisterTimeRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(userregistertimerequestdtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UserRegisterTimeRequestDto_001() {
		Logger.start(true,"批量查询用户注册时间成功");
		try {
			List<Map> list=new LoginHistoryWapper().selectLastUserRegHistory();
			Logger.debug(list.get(0));
			int userid=Integer.valueOf(list.get(0).get("UserId").toString());
			YmtUsers user = new YmtUsersWapper().getYmtUsers(Integer.valueOf(userid));
			String createtime=YMTDateUtil.getFormatTime(user.getDaddtime(),"yyyy-MM-dd HH:mm:ss");
			List<Integer> UserIds=new ArrayList<Integer>();
			UserIds.add(userid);
			userregistertimerequestdtoBean.setUserIds(UserIds);
			userregistertimerequestdtoCall.setData(userregistertimerequestdtoBean);
			userregistertimerequestdtoCall.callService();			
			Logger.verifyEquals("200", userregistertimerequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true", userregistertimerequestdtoCall.getString("Success"), "验证Success");
//			同步时间不正确，有偏差
//			Logger.verifyIsContains(createtime, userregistertimerequestdtoCall.getResult().get(0).toString(), "验证createtime");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_UserRegisterTimeRequestDto_002() {
		Logger.start(true,"缺少参数UserIds,失败");
		try {
			userregistertimerequestdtoCall.setData(userregistertimerequestdtoBean);
			userregistertimerequestdtoCall.callService();			
			Logger.verifyEquals("100", userregistertimerequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("false", userregistertimerequestdtoCall.getString("Success"), "验证Success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_UserRegisterTimeRequestDto_003() {
		Logger.start(true,"批量查询多个用户注册时间成功");
		try {
			List<Map> list=new LoginHistoryWapper().selectLastUserRegHistory();
			YmtUsersWapper userwapper = new YmtUsersWapper();
			List<Integer> UserIds=new ArrayList<Integer>();
			Map createtimes=new HashMap<>();
			for (int i=0;i<10;i++) {
				Map map=list.get(i);
				String userid=map.get("UserId").toString();
				YmtUsers user = userwapper.getYmtUsers(Integer.valueOf(userid));
				String createtime=YMTDateUtil.getFormatTime(user.getDaddtime(),"yyyy-MM-dd HH:mm:ss");
				UserIds.add(Integer.valueOf(userid));
				createtimes.put(userid, createtime);
			}
			
			userregistertimerequestdtoBean.setUserIds(UserIds);
			userregistertimerequestdtoCall.setData(userregistertimerequestdtoBean);
			userregistertimerequestdtoCall.callService();			
			Logger.verifyEquals("200", userregistertimerequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true", userregistertimerequestdtoCall.getString("Success"), "验证Success");
			JSONArray lists = userregistertimerequestdtoCall.getResult();
			for(int i=0;i<lists.length();i++){
				JSONObject result = lists.getJSONObject(i);
				String createtime = (String) createtimes.get(result.get("UserId").toString());
//				同步时间不正确，有偏差
//				Logger.verifyEquals(createtime, result.getString("RegisterTime"), result.get("UserId").toString()+"验证RegisterTime");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_UserRegisterTimeRequestDto_004() {
		Logger.start(true,"批量查询多个用户注册时间数量超限失败");
		try {
			List<Map> list=new LoginHistoryWapper().selectLastUserRegHistory();
			YmtUsersWapper userwapper = new YmtUsersWapper();
			List<Integer> UserIds=new ArrayList<Integer>();
			Map createtimes=new HashMap<>();
			for (int i=0;i<102;i++) {
				UserIds.add(i);
			}
			
			userregistertimerequestdtoBean.setUserIds(UserIds);
			userregistertimerequestdtoCall.setData(userregistertimerequestdtoBean);
			userregistertimerequestdtoCall.callService();			
			Logger.verifyEquals("100", userregistertimerequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("false", userregistertimerequestdtoCall.getString("Success"), "验证Success");
//			Logger.verifyIsContains("参数非法", userregistertimerequestdtoCall.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}